package com.bionet.ssoservice.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bionet.service_base.utils.Result;
import com.bionet.ssoservice.entity.AclUser;
import com.bionet.ssoservice.mapper.AclUserMapper;
import com.bionet.ssoservice.service.AclUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author hzx
 * @since 2021-09-19
 */
@Service
public class AclUserServiceImpl extends ServiceImpl<AclUserMapper, AclUser> implements AclUserService {

    @Override
    public Result login(AclUser aclUser) {
        QueryWrapper<AclUser> aclUserQueryWrapper = new QueryWrapper<>();
        aclUserQueryWrapper.eq("username", aclUser.getUsername());
        AclUser user = baseMapper.selectOne(aclUserQueryWrapper);
        if (SaSecureUtil.md5(aclUser.getPassword()).equals(user.getPassword())) {
            return Result.ok();
        } else {
            return Result.error().message("密码错误");
        }
    }

    @Override
    public Result register(AclUser aclUser) {
        aclUser.setPassword(SaSecureUtil.md5(aclUser.getPassword()));
        try {
            int insert = baseMapper.insert(aclUser);
        } catch (Exception e) {
            return Result.error().message(e.getMessage());
        }
        return Result.ok();
    }
}
